package com.hongdee.crm.wo.service.impl;

import com.hongdee.common.util.TokenUtil;
import com.hongdee.crm.wo.dao.ITCrmWoClaimDao;
import com.hongdee.crm.wo.entity.TCrmWoClaim;
import com.hongdee.crm.wo.service.ITCrmWoClaimService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 侯念晨 on 2017-05-10.
 */
@Service
public class TCrmWoClaimServiceImpl implements ITCrmWoClaimService{
    @Autowired
    private ITCrmWoClaimDao tCrmWoClaimDao;
    private String tabName = "T_CRM_WO_SP_TYPE";

    /**
     *
     * 详情查询
     */
    @Override
    public Map info(String id) throws Exception {
        String sql = "select * from " + tabName + " where id = ? ";
        return this.tCrmWoClaimDao.info(sql, id);
    }

    /**
     * 分页查询
     */
    @Override
    public Map newPage(String start, String limit,String oid) throws Exception{
        String sql="select * from T_CRM_WO_SP_TYPE where company_id = ?  order by  status desc,disp_order asc";
         List<Object> params = new ArrayList();
        /**
         * 把公司ID加入params中
         */
        params.add(oid);
        /**
         * 返回
         */
        return this.tCrmWoClaimDao.page(sql,params.toArray(),start,limit);

    }
    /*
    * 启用停用
   *
    * 请求参数:uid    登录用户 ID
              token  登录用户 token
               id     记录主键

      返回值: success:请求状态，true|false
              msg: 请求结果信息，
   * */
    @Override
    public void update(String id, String status) throws Exception {
        if(null==id||id.equals("")){
            throw new Exception("请选择记录");
        }
        String hql = "update TCrmWoClaim set status = :status where id = :id ";
        Map params = new HashMap();
        params.put("id", id);
        params.put("status", status);
        this.tCrmWoClaimDao.update(hql, params);
    }
    /*
    *    保存工单对应的类别
    *
    *    请求参数  : uid    登录用户 ID
                     token  登录用户 token
                     data  TCrmWoType实体的信息

           返回值 : success:请求状态，true|false
                    msg: 请求结果信息
    * */
    @Override
    public TCrmWoClaim saveOrUpdate(TCrmWoClaim tCrmWoClaim, String uid) throws Exception {
        /*
        * 根据uid获取公司id
        * */
        String oid= TokenUtil.getCompanyId(uid);
        /**
         * 把公司id赋值给实体类
         * */
        tCrmWoClaim.setCompanyId(oid);
        /*
        * 把createUser赋给实体
        * */
        tCrmWoClaim.setCreateUser(uid);
        this.tCrmWoClaimDao.saveOrUpdate(tCrmWoClaim);
        return  tCrmWoClaim;
    }
}
